package com.cyt.work.wb.service.impl;

import cn.hutool.core.date.DateUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.StringUtils;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.work.wb.dao.WbRepairRecordDao;
import com.cyt.work.wb.model.po.WbRepairRecord;
import com.cyt.work.wb.model.vo.WbRepairRecordVo;
import com.cyt.work.wb.service.WbRepairRecordService;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 维修记录表-维修记录表Service层实现类
 *
 * @author shengQiang Yu
 * @date 2019-11-28 9:27:55
 */
@Service("wbRepairRecordService")
public class WbRepairRecordServiceImpl extends BaseServiceImpl<WbRepairRecord> implements WbRepairRecordService {

    @Resource
    private WbRepairRecordDao wbRepairRecordDao;

    @Autowired
    public void setBaseDao(WbRepairRecordDao wbRepairRecordDao) {
        this.baseDao = wbRepairRecordDao;
    }

    /**
     * 查询对象集合
     *
     * @param wbRepairRecordVo 对象
     * @return List 对象集合
     * @author shengQiang Yu
     * @date 2019-11-28 9:27:55
     */
    @Override
    public List<WbRepairRecord> listExample(WbRepairRecordVo wbRepairRecordVo) {
        Example example = new Example(WbRepairRecord.class);
        example.setOrderByClause(" create_time desc");
        Example.Criteria criteria = example.createCriteria();

        if (StringUtils.isNotBlank(wbRepairRecordVo.getRepairId())) {
            criteria.andEqualTo("repairId", wbRepairRecordVo.getRepairId());
        }
        if (StringUtils.isNotBlank(wbRepairRecordVo.getApplyId())) {
            criteria.andEqualTo("applyId", wbRepairRecordVo.getApplyId());
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        return wbRepairRecordDao.selectByExample(example);
    }

    /**
     * 分页查询
     *
     * @param wbRepairRecordVo 接受参数对象
     * @param pageNum          分页数
     * @param pageSize         每页显示条数
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:27:55
     */
    @Override
    public ReturnResult page(WbRepairRecordVo wbRepairRecordVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<WbRepairRecord> list = listExample(wbRepairRecordVo);
        PageInfo<WbRepairRecord> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param wbRepairRecordVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:27:55
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(WbRepairRecordVo wbRepairRecordVo, SysUser sysUser) {
        if (StringUtils.isBlank(wbRepairRecordVo.getId())) {
            WbRepairRecord wbRepairRecord = new WbRepairRecord(sysUser);
            wbRepairRecord.setApplyId(wbRepairRecordVo.getApplyId());
            wbRepairRecord.setRepairId(wbRepairRecordVo.getRepairId());
            wbRepairRecord.setServicerId(wbRepairRecordVo.getServicerId());
            wbRepairRecord.setRepairResult(wbRepairRecordVo.getRepairResult());
            wbRepairRecord.setStatus(wbRepairRecordVo.getStatus());
            wbRepairRecord.setEquipRecord(wbRepairRecordVo.getEquipRecord());
            wbRepairRecord.setErrorRecord(wbRepairRecordVo.getErrorRecord());
            wbRepairRecord.setImages(wbRepairRecordVo.getImages());
            wbRepairRecord.setRepairmanId(wbRepairRecordVo.getRepairmanId());
            wbRepairRecord.setRepaimanName(wbRepairRecordVo.getRepaimanName());
            wbRepairRecord.setRepaimanPhone(wbRepairRecordVo.getRepaimanPhone());
            wbRepairRecordDao.insert(wbRepairRecord);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            WbRepairRecord wbRepairRecord = wbRepairRecordDao.selectByPrimaryKey(wbRepairRecordVo.getId());
            wbRepairRecord.setApplyId(wbRepairRecordVo.getApplyId());
            wbRepairRecord.setRepairId(wbRepairRecordVo.getRepairId());
            wbRepairRecord.setStatus(wbRepairRecordVo.getStatus());
            wbRepairRecord.setServicerId(wbRepairRecordVo.getServicerId());
            wbRepairRecord.setRepairResult(wbRepairRecordVo.getRepairResult());
            wbRepairRecord.setEquipRecord(wbRepairRecordVo.getEquipRecord());
            wbRepairRecord.setErrorRecord(wbRepairRecordVo.getErrorRecord());
            wbRepairRecord.setImages(wbRepairRecordVo.getImages());
            wbRepairRecord.setRepairmanId(wbRepairRecordVo.getRepairmanId());
            wbRepairRecord.setRepaimanName(wbRepairRecordVo.getRepaimanName());
            wbRepairRecord.setRepaimanPhone(wbRepairRecordVo.getRepaimanPhone());
            wbRepairRecord.setUpdateTime(new Date());
            wbRepairRecord.setUpdateUser(sysUser.getId());
            wbRepairRecordDao.updateByPrimaryKey(wbRepairRecord);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param wbRepairRecordVo 参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:27:55
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(WbRepairRecordVo wbRepairRecordVo, SysUser sysUser) {
        //要修改的字段
        WbRepairRecord wbRepairRecord = new WbRepairRecord();
        wbRepairRecord.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        wbRepairRecord.setUpdateUser(sysUser.getId());
        wbRepairRecord.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(WbRepairRecord.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wbRepairRecordVo.getId())) {
            criteria.andIn("id", Arrays.asList(wbRepairRecordVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        wbRepairRecordDao.updateByExampleSelective(wbRepairRecord, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param wbRepairRecordVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:27:55
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(WbRepairRecordVo wbRepairRecordVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(WbRepairRecord.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wbRepairRecordVo.getId())) {
            criteria.andIn("id", Arrays.asList(wbRepairRecordVo.getId().split(",")));
        }
        wbRepairRecordDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }
}